/**
 * 
 */
package ua.ws.feedsreader.actions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Result;
import org.openrdf.query.MalformedQueryException;
import org.openrdf.query.QueryEvaluationException;
import org.openrdf.repository.RepositoryException;

import ua.ws.feedsreader.common.FeedEntry;
import ua.ws.feedsreader.common.User;
import ua.ws.feedsreader.db.SemanticManager;
import ua.ws.feedsreader.utils.Log;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author ricardo
 *
 */
@Result(location="feedsreader.jsp",name="success")
public class FeedsReader extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7585398177462542749L;
	private String categoryResult;
	
	//User info
	private String username;
	private String firstname;
	private String lastname;
	private List<String> userCategories ;
	private List<String> categoriesSuggestion = new ArrayList<>();
	private Map<Integer, FeedEntry> suggestionFeeds = new HashMap<>();
	private Map<Integer, FeedEntry> categoryFeeds = new HashMap<>();
 	
	@Override
	public String execute() {
		
		Map<String, Object> session = ActionContext.getContext().getSession();
		Log.debug("ChooseCategories");
		User user = (User) session.get("USER");
		Log.debug("User:"+user.getLogin() );
		SemanticManager semanticManager = new SemanticManager();
		
		try {
			
			userCategories = semanticManager.getUserCategories(user.getLogin());
			if ( userCategories.size() > 0  ) {
				categoriesSuggestion = semanticManager.getCategoriesSugestion(userCategories.get(0), user.getLogin());
				suggestionFeeds = semanticManager.getFeedsSugestion( userCategories.get(0), user.getLogin() );
				categoryFeeds = semanticManager.getCategoryFeeds( userCategories.get(0) );
			}
			
		} catch (RepositoryException | MalformedQueryException | QueryEvaluationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return ERROR;
		}
		
		setUsername( user.getLogin() );
		setFirstname( user.getFirstName() );
		setLastname( user.getLastName() );
		
		return SUCCESS;
	}

	/**
	 * @return the categoryResult
	 */
	public String getCategoryResult() {
		return categoryResult;
	}

	/**
	 * @param categoryResult the categoryResult to set
	 */
	public void setCategoryResult(String categoryResult) {
		this.categoryResult = categoryResult;
	}

	/**
	 * @return the userCategories
	 */
	public List<String> getUserCategories() {
		return userCategories;
	}

	/**
	 * @param userCategories the userCategories to set
	 */
	public void setUserCategories(List<String> userCategories) {
		this.userCategories = userCategories;
	}

	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * @param username the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * @return the firstname
	 */
	public String getFirstname() {
		return firstname;
	}

	/**
	 * @param firstname the firstname to set
	 */
	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	/**
	 * @return the lastname
	 */
	public String getLastname() {
		return lastname;
	}

	/**
	 * @param lastname the lastname to set
	 */
	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	/**
	 * @return the categoriesSugestion
	 */
	public List<String> getCategoriesSuggestion() {
		return categoriesSuggestion;
	}

	/**
	 * @param categoriesSugestion the categoriesSugestion to set
	 */
	public void setCategoriesSugestion(List<String> categoriesSuggestion) {
		this.categoriesSuggestion = categoriesSuggestion;
	}

	/**
	 * @return the sugestionFeeds
	 */
	public Map<Integer, FeedEntry> getSuggestionFeeds() {
		return suggestionFeeds;
	}

	/**
	 * @param sugestionFeeds the sugestionFeeds to set
	 */
	public void setSugestionFeeds(Map<Integer, FeedEntry> suggestionFeeds) {
		this.suggestionFeeds = suggestionFeeds;
	}

	/**
	 * @return the categoryFeeds
	 */
	public Map<Integer, FeedEntry> getCategoryFeeds() {
		return categoryFeeds;
	}

	/**
	 * @param categoryFeeds the categoryFeeds to set
	 */
	public void setCategoryFeeds(Map<Integer, FeedEntry> categoryFeeds) {
		this.categoryFeeds = categoryFeeds;
	}

	/**
	 * @param categoriesSuggestion the categoriesSuggestion to set
	 */
	public void setCategoriesSuggestion(List<String> categoriesSuggestion) {
		this.categoriesSuggestion = categoriesSuggestion;
	}

	/**
	 * @param suggestionFeeds the suggestionFeeds to set
	 */
	public void setSuggestionFeeds(Map<Integer, FeedEntry> suggestionFeeds) {
		this.suggestionFeeds = suggestionFeeds;
	}
	
}
